import Paging from '../../../wxjs/Paging.js'
import navToNext from '../../../wxjs/navToNext.js'
import buy from '../../../wxjs/buy.js'
const app = getApp();

var pag = new Paging();

let tabs = [{
    id: -1,
    text: '全部',
    status: true,
  }, {
    id: 0,
    text: '待付款',
  }, {
    id: 1,
    text: '待使用',
  },
  //  {
  //   id: 2,
  //   text: '已使用',
  // },
  //  {
  //   id: 3,
  //   text: '待评论',
  // },
  {
    id: 7,
    text: '已结束',
  },
  {
    id: 4,
    text: '退款/售后',
  },
  //  {
  //   id: 5,
  //   text: '即将开始',
  // }, {
  //   id: 6,
  //   text: '等位中',
  // }, 
];

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  tabs: [],
  tabId: 1,
  mainColor: app.mainColor,
};

var currentPage = {
  data,
  reqUrl: app.serAddrs.orderList,
  orderParam: {}, //订单请求参数
  payParam: {}, //支付参数
  onLoad,
  loadInit, //跳转登录并成功后触发
  setTabs,
  toggleTab,
  deleteOrder,
  buyPay, //立即支付
  walletPay, //钱包支付
  wxPay, //微信支付前调用
  payment, //微信支付
  doneInputPassword, //6位密码输入完毕
  donePay,
};

var page = Object.assign({}, pag.func(), navToNext, buy, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const tabid = options && options.tabid || -1;
  that.setTabs(tabid);
}

function loadInit() {
  const that = this;
  that.setData({
    noLogin: false,
    noBind: false,
  })
  // 初始化
  that.pageListInit();
}

function setTabs(tabid) {
  const that = this;
  tabs.forEach((item) => {
    if (item.id == tabid) {
      item.status = true;
    } else {
      item.status = false;
    }
  })
  that.setData({
    tabs,
    tabId: tabid,
  })
}

function toggleTab(event) {
  const that = this;
  var id = event.detail.id;
  that.setData({
    tabId: id,
  })
  that.listParams.status = id;
  if (id == -1) that.listParams.status = "";

  app.xlogin.getUser(function(res) {
    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;

    myInfo = app.isNavigateToBind();
    if (!myInfo) return;

    // 初始化
    that.pageListInit();
  })
}

function deleteOrder(event) {
  const that = this;
  const index = event.currentTarget.dataset.index || 0;
  let lists = that.data.lists || [];
  const uuid = lists[index].uuid || '';
  app.middleware.requestAjax({
    serAddr: app.serAddrs.orderDel,
    data: {
      uuid,
    },
    success: function(res) {
      lists.splice(index, 1);
      that.setData({
        lists
      })
    },
    complete: function(res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })
}

function buyPay(event) {
  const that = this;
  const index = event.currentTarget.dataset.index || 0;
  let lists = that.data.lists || [];
  const uuid = lists[index].uuid || '';
  let quantity = that.data.orderQuantity || 1;
  let price = lists[index].price || 0;
  let totalPrice = 0;

  if (quantity < 1 || price * 100 <= 0) return false;

  totalPrice = Number(quantity) * Number(price);

  that.setData({
    totalPrice,
    gymName: lists[index].gymName,
    itemIndex: index,
  })

  that.payParam.uuids = [uuid];
  that.payParam.orderUuids = uuid;

  wx.showActionSheet({
    itemList: ["钱包支付", "微信支付"],
    success: function(res) {
      const tapIndex = res.tapIndex;

      if (tapIndex == 0) {
        that.walletPay();
      } else if (tapIndex == 1) {
        that.wxPay();
      }
    }
  })

}

function walletPay() {
  const that = this;

  if (!app.passIsSet()) return false;

  that.setData({
    payModel: true
  })
}

function wxPay() {
  const that = this;
  app.infoLog(typeof that.payParam.uuids)
  // if ((typeof that.payParam.uuids) == "object"){
  //   that.payParam.uuids = JSON.stringify(that.payParam.uuids);
  // }
  app.middleware.requestAjax({
    serAddr: app.serAddrs.wxPay,
    data: {
      orderUuids: that.payParam.orderUuids,
    },
    method: 'GET',
    cancelToast: true,
    success: function(res) {
      that.payment(res.data)
    },
    complete: function(res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })

}

function payment(data) {
  const that = this;

  wx.requestPayment({
    timeStamp: data.timeStamp,
    nonceStr: data.nonceStr,
    package: data.prepayId,
    signType: data.signType,
    paySign: data.paySign,
    success(res) {
      app.infoLog(res)
      that.donePay();
    },
    fail(res) {
      app.infoLog(res)
    },
    complete: function(res) {
      app.infoLog(res)
    }
  })
}

function doneInputPassword(event) {
  const that = this;
  const password = event.detail;
  that.payParam.payPass = password;
  app.infoLog(typeof that.payParam.uuids)
  // if ((typeof that.payParam.uuids) == "string") {
  //   that.payParam.uuids = JSON.parse(that.payParam.uuids);
  // }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.walletPay,
    data: that.payParam,
    cancelToast: true,
    success: function(res) {
      that.donePay();
    },
    complete: function(res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })
}

function donePay() {
  const that = this;
  const tabId = that.data.tabId || 1;
  const itemIndex = that.data.index || 0;
  let lists = that.data.lists || [];

  if (tabId == 2) {
    lists.splice(itemIndex, 1);
  } else {
    lists[itemIndex].status = 1;
    lists[itemIndex].couldPayStatus = 0;
    lists[itemIndex].paymentStatus = "待使用";
  }

  that.setData({
    lists,
  })
}